import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from page_model.base_page import Base
from tools.tools import Tools


class ReceivingMail(Base):

    def __init__(self):
        chromeOptions = webdriver.ChromeOptions()
        #  改动下载地址
        prefs = {"download.default_directory": r"C:\Users\Administrator\Desktop\text2"}
        chromeOptions.add_experimental_option("prefs", prefs)
        chromedriver = "./chromedriver"
        self.driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)
        self.driver.get('https://mail.163.com/')

    def login(self, name, pwd):
        """
        登录的方法
        :param name: 登录的用户名
        :param pwd: 登录的密码
        :return: 不返回值，进入到界面内
        """
        #  切入内嵌页中
        self.frame((By.XPATH, '//*[@frameborder="0"]'))
        #  输入用户名
        self.send_keys((By.XPATH, '//*[@data-placeholder="邮箱帐号或手机号码"]'), name)
        #  输入密码
        self.send_keys((By.XPATH, '//*[@id="pwdtext"]'), pwd)
        #  点击登录
        self.click((By.XPATH, '//*[@id="dologin"]'))

    def mail(self):
        #  点击收件箱
        self.click((By.XPATH, '//*[@title="收件箱"]'))
        #  点击最近一条邮件
        self.click((By.XPATH, '//*[@id="_dvModuleContainer_mbox.ListModule_0"]/div/div/div/div[5]/div[2]'))
        #  获取各个字体
        b = self.get_text((By.XPATH, '//*[@class="nui-addr-email"]'))  # 发件人
        c = self.get_text((By.XPATH, '//*[@title="邮件标题"]'))  # 主题
        self.frame((By.XPATH, '/html/body/div[2]/div[1]/div[3]/div/div[1]/div[6]/div/iframe'))
        d = self.get_text((By.XPATH, '//*[@id="content"]/div[1]/p'))  # 内容
        self.parent_frame()
        e = self.get_text((By.XPATH, '//*[@class="ig0 eu0"]/div[2]'))  # 日期
        ls = [b, c, d, e]
        return ls

    def dow(self, address, name):
        """
        点击下载的方法
        :param address:地址
        :param name:文件
        :return:不返回东西
        """
        ac = ActionChains(self.driver)
        x1 = self.find_element((By.XPATH, '//*[@class="js-component-icon nui-ico nui-ico-file75  nui-ico-file75-7  "]'))
        ac.move_to_element(x1).perform()
        time.sleep(1)
        self.click((By.LINK_TEXT, '下载'))
        Tools.judge(address, name)

if __name__ == '__main__':
    a = ReceivingMail()